package com.dhf.framework.common;

import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;

public class RequestParamToMap {
	
	public static final Map<String,Object> convert(HttpServletRequest request){
		Map<String,Object> param=new HashMap<String, Object>();
		Enumeration<String> e=request.getParameterNames();
		String name="";
		String str[]=null;
		while(e.hasMoreElements()){
			name=e.nextElement();
			str=request.getParameterValues(name);
			name=name.replace("[", "").replace("]", "");
			if(str.length==1)
				param.put(name, str[0].replaceAll("\"", "\\\"").replaceAll("\'", "\\\'").replaceAll("<", "<").replaceAll(">", ">"));
			else{
				for(int i=0;i<str.length;i++)
					str[i]=str[i].replaceAll("\"", "\\\"").replaceAll("\'", "\\\'").replaceAll("<", "<").replaceAll(">", ">");
				param.put(name, str);
			}
		}
		return param;
	}
	
	public static Map<String,Object> coventValues(Map<String,Object> map,List<Map<String, Object>> list){
		Map<String,Object> returnMap=new HashMap<String,Object>();
		for(Map<String, Object> attrMap:list){
			String columnName=MapUtils.getString(attrMap, "column_name");
			String dataType=MapUtils.getString(attrMap, "DATA_TYPE");
			if(map.containsKey(columnName)&&StringUtils.isNotEmpty(MapUtils.getString(map, columnName))){
				if("datetime".equals(dataType)){
					Date date=new Date(MapUtils.getString(map, columnName));
					returnMap.put(columnName, date);
				}
			}
			
		}
		
		
		
		return map;
	}
	
	public static Map getParameterMap(HttpServletRequest request) {  
		// 参数Map  
		Map<String, String[]> properties = request.getParameterMap();  
	    // 返回值Map  
	    Map returnMap = new HashMap();  
	    Iterator entries = properties.entrySet().iterator();  
	    Map.Entry entry;  
	    String name = "";  
	    String value= "";  
	    while (entries.hasNext()) {  
	        entry = (Map.Entry) entries.next();  
	        name = (String) entry.getKey();  
	        Object valueObj = entry.getValue();  
	        if(null == valueObj){  
	            value = "";  
	        }else if(valueObj instanceof String[]){  
	            String[] values = (String[])valueObj;  
	            for(int i=0;i<values.length;i++){  
	                value = values[i] + ",";  
	            }  
	            value = value.substring(0, value.length()-1);  
	        }else{  
	            value = valueObj.toString();  
	        }  
	        returnMap.put(name, value);  
	    }
		return returnMap;  
    }
}
 